package com.example.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.user.entity.CrmBanner;
import com.example.user.service.CrmBannerService;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/cms/banner")
@CrossOrigin
public class CrmBannerAdminController {

    @Autowired
    CrmBannerService crmBannerService;


    @PostMapping("saveBanner")
    public Result saveBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return Result.ok();
    }

    @DeleteMapping("deleteBanner/{id}")
    public Result deleteBanner(@PathVariable String id){
        crmBannerService.removeById(id);
        return Result.ok();
    }

    @PutMapping("updateBanner")
    public Result updateBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.updateById(crmBanner);
        return Result.ok();
    }

    @GetMapping("getBanner/{current}/{size}")
    public Result getBanner(@PathVariable Long current,@PathVariable Long size){
        Page<CrmBanner> bannerPage = new Page<>(current,size);
        crmBannerService.page(bannerPage,null);
        List<CrmBanner> bannerList = bannerPage.getRecords();
        long total = bannerPage.getTotal();
        return Result.ok().data("bannerList",bannerList).data("total",total);
    }


}

